package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;
import com.cn.wanxi.util.MyDate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String sql = "insert into product (" +
                "name," +
                "normal_price," +
                "price," +
                "content," +
                "is_show," +
                "is_recommend," +
                "img_href," +
                "cuisine_id," +
                "create_time," +
                "update_time)" +
                " values ('" + productModel.getName() + "'," +
                "" + productModel.getNomalPrice() + "," +
                "" + productModel.getMarketPrice() + "," +
                "'" + productModel.getDiscrib() + "'," +
                "" + productModel.getEnable() + "," +
                "1," +
                "'" + productModel.getImgHref() + "'," +
                "" + productModel.getCookTypeId() + "," +
                "'" + simpleDateFormat.format(new Date()) + "'," +
                "'" + simpleDateFormat.format(new Date()) + "')";
        return JDBC.update(sql);
    }

    @Override
    public List<ProductModel> findAll(ProductModel model) {
        String sql = "select p.*,c.name  cookTypeName,c.id  cookTypeId from product p ,cuisine  c" +
                " where p.cuisine_id=c.id ";
        if (model.getCookTypeId() != 0) {
            sql += " and c.id=" + model.getCookTypeId();
        }
        if (model.getName() != null && !"".equals(model.getName())) {
            sql += " and p.name like '%" + model.getName() + "%'";
        }
//        价格
        if (model.getStartNormalPrice() != 0.0 && model.getEndNormalPrice() == 0.0) {
            sql += " and p.normal_price >='" + model.getStartNormalPrice() + "'";
        }
        if (model.getStartNormalPrice() == 0.0 && model.getStartNormalPrice() != 0.0) {
            sql += " and p.normal_price <='" + model.getStartNormalPrice() + "'";
        }
        if (model.getStartNormalPrice() != null && model.getStartNormalPrice() != 0.0 && model.getEndNormalPrice() != null && model.getEndNormalPrice() != 0.0) {
            sql += " and p.normal_price between  '" + model.getStartNormalPrice() + "' and '" + model.getEndNormalPrice() + "'";
        }
        if (model.getDiscrib() != null && !"".equals(model.getDiscrib())) {
            sql += " and p.content like '%" + model.getDiscrib() + "%'";
        }
        sql += " order by id desc " +
                "limit " + model.getPageModel().getStartSize() + "," + model.getPageModel().getPageSize();
        ResultSet resultSet = JDBC.find(sql);
        return setModel(resultSet);
    }

    @Override
    public int getCount(ProductModel model) {
        String sql = "select count(*) count from product p ,cuisine  c" +
                " where p.cuisine_id=c.id ";
        if (model.getCookTypeId() != 0) {
            sql += " and c.id=" + model.getCookTypeId();
        }
        if (model.getName() != null && !"".equals(model.getName())) {
            sql += " and p.name like '%" + model.getName() + "%'";
        }
//        价格
        if (model.getStartNormalPrice() != 0.0 && model.getEndNormalPrice() == 0.0) {
            sql += " and p.normal_price >='" + model.getStartNormalPrice() + "'";
        }
        if (model.getStartNormalPrice() == 0.0 && model.getStartNormalPrice() != 0.0) {
            sql += " and p.normal_price <='" + model.getStartNormalPrice() + "'";
        }
        if (model.getStartNormalPrice() != null && model.getStartNormalPrice() != 0.0 && model.getEndNormalPrice() != null && model.getEndNormalPrice() != 0.0) {
            sql += " and p.normal_price between  '" + model.getStartNormalPrice() + "' and '" + model.getEndNormalPrice() + "'";
        }
        if (model.getDiscrib() != null && !"".equals(model.getDiscrib())) {
            sql += " and p.content like '%" + model.getDiscrib() + "%'";
        }
        ResultSet resultSet = JDBC.find(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update product set name='" + productModel.getName() + "',";

        if (!"undefined".equals(productModel.getImgHref())) {
            sql += " img_href='" + productModel.getImgHref() + "',";
        }
        if (!"".equals(productModel.getDiscrib())) {
            sql += " content='" + productModel.getDiscrib() + "',";
        }
        sql += " update_time='" + MyDate.getDate() + "' where id=" + productModel.getId();

        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(int id) {
        String sql = "select p.*,c.name  cookTypeName,c.id cookTypeId  from product p ,cuisine  c" +
                " where p.cuisine_id=c.id  and p.id=" + id;
        ResultSet resultSet = JDBC.find(sql);
        List<ProductModel> list = setModel(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    private List<ProductModel> setModel(ResultSet resultSet) {
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setMarketPrice(resultSet.getDouble("price"));
                productModel.setNomalPrice(resultSet.getDouble("normal_price"));
                productModel.setDiscrib(resultSet.getString("content"));
                productModel.setEnable(resultSet.getInt("is_show"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setCookTypeName(resultSet.getString("cookTypeName"));
                productModel.setCreateTime(resultSet.getString("create_time"));
                productModel.setCookTypeId(resultSet.getInt("cookTypeId"));
                list.add(productModel);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

